<template>
  <el-dialog :visible.sync="dialogFormVisible" width="500px" @close="close">
    <template slot="title">
      <div>
        <svg-icon icon-class="licenseManage" />
        <span>{{ title }}</span>
      </div>
    </template>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item
        v-for="(value, key, index) in license"
        :key="index"
        :label="value"
        :prop="key"
      >
        <el-input v-model.trim="form[key]" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  const license = {
    licenseName: '许可名称',
    licenseCount: '数量',
    usedLicenseCount: '已用数量',
    licenseStartTime: '有效时间',
    status: '状态',
  }

  export default {
    name: 'UserLincenseEdit',
    data() {
      return {
        license: license,
        form: {},
        rules: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
          this.form = {}
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.dialogFormVisible = false
        this.$refs['form'].clearValidate()
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            let obj = Object.assign({}, this.form)
            this.close()
            this.$emit('fetch-data', obj)
          } else {
            return false
          }
        })
      },
    },
  }
</script>
